package mkv.MyGUI;
import processing.core.*; 

public class MyGUIButtonDrag extends MyGUIButton {
  
  private int xoff, yoff;  // Used to track mouse offset so that object isn't dragged from centre
  private int minAreax, minAreay, maxAreax, maxAreay; // constrain object's drag area

  /* Constructors */
  public MyGUIButtonDrag(PApplet root, int x, int y, int width, int height) {
    super(root, x, y, width, height);  
  }

  public MyGUIButtonDrag(PApplet root, int x, int y, PImage icon) {
    super(root, x, y, icon);
  }
      
  public void clearDragArea() {
    setDragArea(0, 0, 0, 0);
  }
  
  public void setDragArea(int minx, int miny, int maxx, int maxy) {
    minAreax = minx; minAreay = miny;
    maxAreax = maxx; maxAreay = maxy;
  }
  
  public void mousePressed() {
    if(hasFocus()) {
      dragged = true;
      xoff = _parent.tmouseX - _x;
      yoff = _parent.tmouseY - _y;
    }
  }
   
  public void mouseDragged() {
    if(dragged) {
      _x = _parent.tmouseX - xoff;
      _y = _parent.tmouseY - yoff;
      // This logic reads: If all these values are not 0 Then constrain the _x, _y position
      if(!(minAreax == 0 && minAreay == 0 && maxAreax == 0 && maxAreay == 0)) {
        _x = PApplet.constrain(_x, minAreax, maxAreax);
        _y = PApplet.constrain(_y, minAreay, maxAreay);
      }
    }
  }
}
